import pandas as pd
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold
from tiancheng.base.base_helper import *

op_train = get_operation_train_new()
trans_train = get_transaction_train_new()

op_test = get_operation_round1_new()  # pd.read_csv('input/operation_round1_new.csv')
trans_test = get_transaction_round1_new()  # pd.read_csv('input/transaction_round1_new.csv')
y = get_tag_train_new()  # pd.read_csv('input/tag_train_new.txt')
sub = get_sub()


def get_feature(op, trans, label):
    for feature in op.columns[2:]:
        label = label.merge(op.groupby(['UID'])[feature].count().reset_index(), on='UID', how='left')
        label = label.merge(op.groupby(['UID'])[feature].nunique().reset_index(), on='UID', how='left')

    for feature in trans.columns[2:]:
        if trans_train[feature].dtype == 'object':
            label = label.merge(trans.groupby(['UID'])[feature].count().reset_index(), on='UID', how='left')
            label = label.merge(trans.groupby(['UID'])[feature].nunique().reset_index(), on='UID', how='left')
        else:
            label = label.merge(trans.groupby(['UID'])[feature].count().reset_index(), on='UID', how='left')
            label = label.merge(trans.groupby(['UID'])[feature].nunique().reset_index(), on='UID', how='left')
            label = label.merge(trans.groupby(['UID'])[feature].max().reset_index(), on='UID', how='left')
            label = label.merge(trans.groupby(['UID'])[feature].min().reset_index(), on='UID', how='left')
            label = label.merge(trans.groupby(['UID'])[feature].sum().reset_index(), on='UID', how='left')
            label = label.merge(trans.groupby(['UID'])[feature].mean().reset_index(), on='UID', how='left')
            label = label.merge(trans.groupby(['UID'])[feature].std().reset_index(), on='UID', how='left')
    # label = label.T.drop_duplicates(subset=None, keep='first', inplace=True)
    return label

# train = pd.DataFrame()
# test = pd.DataFrame()
# train, test = get_is_merchant_ftr(trans_train, trans_test, sub, y, train, test)

train = get_feature(op_train, trans_train, y).fillna(-1)
test = get_feature(op_test, trans_test, sub).fillna(-1)
label = y['Tag']
test_id = test['UID']
cols = list(train.columns.values)
# [cols.remove(item) for item in tag_header]
cols = [str(i) + item for i, item in enumerate(cols[2:])]
train.columns = tag_header + cols
test.columns = tag_header + cols
train, test = woe_all(train, test, train['Tag'], cols)
train = train.fillna(method="ffill")
test = test.fillna(method="ffill")
# 后向填充，使用下一行的值,不存在的时候就不填充
train = train.fillna(method="bfill")
test = test.fillna(method="bfill")
# test = test.fillna(-1)
# train = train.fillna(-1)
train = train[features_select_cols02]
test = test[features_select_cols02]

test01 = get_test_weo()
train01 = get_train_weo()
train[tag_hd.UID] = y[tag_hd.UID]
# train[tag_hd.Tag] = y[tag_hd.Tag]
test[tag_hd.UID] = sub[tag_hd.UID]
# test[tag_hd.Tag] = sub[tag_hd.Tag]
# train.pop(tag_hd.Tag)
# test.pop(tag_hd.Tag)
train = train01.merge(train, on='UID', how='left')
test = test01.merge(test, on='UID', how='left')
train = train[tag_header + features_name_all]
test = test[features_name_all]
print(test.shape)
print(train.shape)
train, test = get_is_merchant_ftr(trans_train, trans_test, sub, y, train, test)
train.to_csv(features_base_path + "train_data.csv", index=False)
test.to_csv(features_base_path + "test_data.csv", index=False)
train = train.drop(['UID', 'Tag'], axis=1)
test = test.drop(['UID', 'Tag'], axis=1)

lgb_model = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=100, reg_alpha=3, reg_lambda=5, max_depth=-1,
                               n_estimators=5000, objective='binary', subsample=0.95, colsample_bytree=0.77,
                               subsample_freq=1, learning_rate=0.05,
                               random_state=1000, n_jobs=16, min_child_weight=4, min_child_samples=5, min_split_gain=0)
skf = StratifiedKFold(n_splits=10, random_state=2018, shuffle=True)
best_score = []
print(train.shape)
print(test.shape)
oof_preds = np.zeros(train.shape[0])
sub_preds = np.zeros(test_id.shape[0])

for index, (train_index, test_index) in enumerate(skf.split(train, label)):
    lgb_model.fit(train.iloc[train_index], label.iloc[train_index], verbose=10,
                  eval_set=[(train.iloc[train_index], label.iloc[train_index]),
                            (train.iloc[test_index], label.iloc[test_index])], early_stopping_rounds=50)
    best_score.append(lgb_model.best_score_['valid_1']['binary_logloss'])
    print(best_score)
    oof_preds[test_index] = lgb_model.predict_proba(train.iloc[test_index], num_iteration=lgb_model.best_iteration_)[:,
                            1]

    test_pred = lgb_model.predict_proba(test, num_iteration=lgb_model.best_iteration_)[:, 1]
    sub_preds += test_pred / 10
    # print('test mean:', test_pred.mean())
    # predict_result['predicted_score'] = predict_result['predicted_score'] + test_pred

m = tpr_weight_funtion(y_predict=oof_preds, y_true=label)
sub = get_sub()
sub['Tag'] = sub_preds
sub.to_csv(sub_base_path + 'baseline_%s.csv' % str(m), index=False)
print(m)
